﻿using System;
using System.Collections.Generic;
using System.Text;
using TTEngine;

public class Main
{
    Framework.handle_Material mat;
    Framework.handle_Mesh mesh;
    Framework.handle_Material mat2;
    Framework.handle_Mesh mesh2;
    Framework.handle_Texture texwhite;
    int mainwinid = 0;
    float[] matview = new float[16];
    float[] matproj = new float[16];
    float[] matmodel = new float[16];
    //
    bool mousedown = false;
    int dragwin = -1;
    int dragx = 0;
    int dragy = 0;

    public Main()
    {
        init();
    }

    private void init()
    {
        //初始化
        mat.ptr = 0;
        mesh.ptr = 0;
        mat2.ptr = 0;
        mesh2.ptr = 0;
        texwhite.ptr = 0;

        //打印log
        Framework.APP_SetLogger_safe((txt) =>
        {
            Console.WriteLine(txt);
        });

        //主循环启动
        Framework.App_MainLoop("hello window", OnInit, OnUpdate, OnWinRender, OnExit);
    }

    unsafe void OnInit()
    {
        mainwinid = Framework.Window_GetMainWinID();
        mat = Framework.Material_CreateDefault();
        mesh = Framework.Mesh_CreateSample();
        mat2 = Framework.Material_CreateDefaultTexture();
        mesh2 = dotnet.loader.MeshTool.CreateRect();
        texwhite = Framework.Texture_Create(2, 2);
        unsafe
        {
            byte[] data = new byte[16];
            for (var i = 0; i < data.Length; i++)
            {
                data[i] = (byte)255;
            }
            fixed (byte* ptr = data)
            {
                Framework.XYWH xywh = new Framework.XYWH();
                xywh.x = 0;
                xywh.y = 0;
                xywh.w = 2;
                xywh.h = 2;
                Framework.Texture_Upload(texwhite, xywh, ptr, 16);
            }
        }
        var charLen = 256;
        var _cBuf = new char[charLen];
        string u0name = Framework.Material_GetUniformName_safe(mat2, 0, new string(_cBuf), charLen);
        if (u0name == "s_tex")
        {
            //Framework.Material_SetTexture(mat2, 1, texwhite);
        }

        int win2 = Framework.Window_Create("c#2", true);
        Framework.Window_SetPos(win2, 100, 100, 400, 300);

        int win3 = Framework.Window_Create("c#3", true);
        Framework.Window_SetPos(win3, 200, 200, 400, 300);

        float[] eye = new float[] { 0, 0, -5 };
        float[] to = new float[] { 0, 0, 0 };

        unsafe
        {
            fixed (float* _matview = matview, _eye = eye, _to = to, _matproj = matproj, _matmodel = matmodel)
            {
                Framework.Matrix_GenViewLookAt(_matview, _eye, _to);
                Framework.Matrix_GenOrtho(_matproj, -1, 1, -1, 1, 0, 100);
                Framework.Matrix_GenIdentity(_matmodel);
            }
        }
    }

    void OnUpdate()
    {
        //resize window

        if (dragwin > 0)
        {
            int bts = 0;
            int[] mxArr = { 0 }, myArr = { 0 };
            unsafe
            {
                fixed (int* _mx = &mxArr[0], _my = &myArr[0])
                {
                    bts = Framework.GetMousePos(_mx, _my);
                }
            }
            int mx = mxArr[0], my = myArr[0];
            if (bts == 0)
            {
                //������ɿ�
                dragwin = -1;
            }
            else
            {
                int x = mx - dragx;
                int y = my - dragy;
                Framework.Window_SetPos(dragwin, x, y, -1, -1);
            }
        }
        else
        {
            int winid = Framework.GetFocusWindowID();
            if (winid >= 0 && winid != mainwinid)
            {
                int bts = 0;
                int[] mxArr = { 0 }, myArr = { 0 };
                unsafe
                {
                    fixed (int* _mx = &mxArr[0], _my = &myArr[0])
                    {
                        bts = Framework.GetMousePos(_mx, _my);
                    }
                }
                int mx = mxArr[0], my = myArr[0];

                if (bts > 0)
                {
                    int x = 0, y = 0, w = 0, h = 0;
                    Framework.Window_GetPos(winid, ref x, ref y, ref w, ref h);
                    dragx = mx - x;
                    dragy = my - y;
                    dragwin = winid;

                }
            }
        }
    }

    void OnWinRender(int winid)
    {
        if (winid == mainwinid)
        {
            Framework.Frame_Clear(0x11aabbff, true, true);

            Framework.Matrix_Test();
            Framework.Frame_Draw(mat, mesh);
        }
        else
        {
            Framework.Frame_Clear(0x00aa33ff, true, true);

            Framework.Material_SetTexture(mat2, 0, texwhite);

            unsafe
            {
                fixed (float* _matview = matview, _matproj = matproj, _matmodel = matmodel)
                {
                    Framework.Matrix_SetViewProj(_matview, _matproj);
                    Framework.Matrix_SetModel(_matmodel);
                }
            }

            Framework.Frame_Draw(mat2, mesh2);
        }
    }

    void OnExit()
    {

    }
}
